package saxion.speelveld.spaceengineer.controller;

import java.util.ArrayList;

import saxion.speelveld.spaceengineer.R;
import saxion.speelveld.spaceengineer.model.Building;
import saxion.speelveld.spaceengineer.model.ProducingBuilding;
import saxion.speelveld.spaceengineer.model.Resource;
import saxion.speelveld.spaceengineer.model.ResourceWrapper;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class BuildingRowAdapter extends BaseAdapter
{

	private Activity activityContext;

	private ArrayList<Building> buildings;

	private static LayoutInflater inflater = null;

	private int countAccassed;

	private ResourceWrapper resources;

	public BuildingRowAdapter(Activity currentActivity, ArrayList<Building> buildingList, ResourceWrapper resources)
	{
		this(currentActivity, buildingList);
		this.resources = resources;
	}

	public BuildingRowAdapter(Activity currentActivity, ArrayList<Building> buildingList)
	{
		activityContext = currentActivity;
		buildings = buildingList;
		inflater = (LayoutInflater) activityContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		countAccassed = 0;
	}

	@Override
	public int getCount()
	{
		return buildings.size();
	}

	@Override
	public Object getItem(int arg0)
	{
		return arg0;
	}

	@Override
	public long getItemId(int position)
	{
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		countAccassed = countAccassed + 1;
		// Log.i("saxion.speelveld.spaceengineer.buildingScreen", "getView -> access count: " + countAccassed);

		if (convertView == null)
		{
			convertView = inflater.inflate(R.layout.list_row, null);
		}

		RelativeLayout rl = (RelativeLayout) convertView.findViewById(R.id.RLbuildingRow);
		// rl.setBackgroundDrawable(activityContext.getResources().getDrawable(R.drawable.gradient_bg_green));

		TextView buildingName = (TextView) convertView.findViewById(R.id.tvBuildingName); // building name
		TextView buildingEx = (TextView) convertView.findViewById(R.id.tvBuildingExplanation); // building explanation
		LinearLayout buildingRes = (LinearLayout) convertView.findViewById(R.id.hllBuildingResources); // building
		LinearLayout buildingProducesPerSecond = (LinearLayout) convertView
			.findViewById(R.id.hllBuildingProducesPerSecond); // building
		LinearLayout buildingProducesOnce = (LinearLayout) convertView.findViewById(R.id.hllBuildingProducesOnce); // building
		// produces
		// resources
		// needed
		ImageView thumb_image = (ImageView) convertView.findViewById(R.id.list_image); // thumb image
		// TextView hiddenId = (TextView) view.findViewById(R.id.tvHiddenBuildingId);
		buildingRes.removeAllViews();
		buildingProducesPerSecond.removeAllViews();
		buildingProducesOnce.removeAllViews();

		buildingName.setText(buildings.get(position).getBuildingName());
		buildingEx.setText(buildings.get(position).getBuildingExplanation());
		// hiddenId.setText(position);

		TextView tvTemp = new TextView(activityContext.getApplicationContext());
		tvTemp.setText("Costs:");
		tvTemp.setTextColor(Color.BLACK);
		tvTemp.setPadding(8, 0, 12, 0);
		buildingRes.addView(tvTemp);
		for (Resource cost : buildings.get(position).resourceCosts)
		{
			Bitmap img = BitmapManager.getBitmapByResourceType(cost.getResourceType(),
				activityContext.getApplicationContext(), 16, 16);
			ImageView imageview = new ImageView(activityContext.getApplicationContext());
			imageview.setImageBitmap(img);
			imageview.setMaxHeight(16);
			imageview.setPadding(14, 8, 4, 0);
			buildingRes.addView(imageview);

			tvTemp = new TextView(activityContext.getApplicationContext());
			tvTemp.setText(cost.getAmount() + " ");
			if (resources.getResourceAmount(cost.getResourceType()) > cost.getAmount())
			{
				tvTemp.setTextColor(Color.BLACK);
			}
			else
			{
				tvTemp.setTextColor(Color.RED);
			}
			buildingRes.addView(tvTemp);
		}

		TextView produces = new TextView(activityContext.getApplicationContext());
		TextView produces2 = new TextView(activityContext.getApplicationContext());

		if (buildings.get(position) instanceof ProducingBuilding)
		{
			ProducingBuilding producerendGebouw = (ProducingBuilding) buildings.get(position);

			if (producerendGebouw.getResourceProducedGainsOverTime().size() > 0)
			{
				produces.setText("Produces per tick: ");
				produces.setTextColor(Color.BLACK);
				produces.setPadding(8, 0, 12, 0);
				buildingProducesPerSecond.addView(produces);
				produces = new TextView(activityContext.getApplicationContext());
				buildingProducesPerSecond.addView(produces);
				for (Resource cost : producerendGebouw.getResourceProducedGainsOverTime())
				{
					Bitmap img = BitmapManager.getBitmapByResourceType(cost.getResourceType(),
						activityContext.getApplicationContext(), 16, 16);
					ImageView imageview = new ImageView(activityContext.getApplicationContext());
					imageview.setImageBitmap(img);
					imageview.setMaxHeight(16);
					imageview.setPadding(14, 8, 4, 0);
					buildingProducesPerSecond.addView(imageview);

					produces = new TextView(activityContext.getApplicationContext());
					produces.setText(cost.getAmount() + " ");
					produces.setTextColor(Color.BLACK);
					buildingProducesPerSecond.addView(produces);

				}
			}
			if (producerendGebouw.getResourceProducedCosts().size() > 0)
			{

				produces = new TextView(activityContext.getApplicationContext());
				buildingProducesPerSecond.addView(produces);
				for (Resource cost : producerendGebouw.getResourceProducedCosts())
				{
					Bitmap img = BitmapManager.getBitmapByResourceType(cost.getResourceType(),
						activityContext.getApplicationContext(), 16, 16);
					ImageView imageview = new ImageView(activityContext.getApplicationContext());
					imageview.setImageBitmap(img);
					imageview.setMaxHeight(16);
					imageview.setPadding(14, 8, 4, 0);
					buildingProducesPerSecond.addView(imageview);

					produces = new TextView(activityContext.getApplicationContext());
					produces.setText("-" + cost.getAmount() + " ");
					produces.setTextColor(Color.BLACK);
					buildingProducesPerSecond.addView(produces);
				}
			}

			if (producerendGebouw.getResourceProducedGainsOnce().size() > 0)
			{
				produces2.setText("Produces once: ");
				produces2.setTextColor(Color.BLACK);
				produces2.setPadding(8, 0, 12, 0);
				buildingProducesOnce.addView(produces2);
				produces2 = new TextView(activityContext.getApplicationContext());
				buildingProducesOnce.addView(produces2);
				for (Resource cost : producerendGebouw.getResourceProducedGainsOnce())
				{
					Bitmap img = BitmapManager.getBitmapByResourceType(cost.getResourceType(),
						activityContext.getApplicationContext(), 16, 16);
					ImageView imageview = new ImageView(activityContext.getApplicationContext());
					imageview.setImageBitmap(img);
					imageview.setMaxHeight(16);
					imageview.setPadding(14, 8, 4, 0);
					buildingProducesOnce.addView(imageview);

					produces = new TextView(activityContext.getApplicationContext());
					produces.setText(cost.getAmount() + " ");
					produces.setTextColor(Color.BLACK);
					buildingProducesOnce.addView(produces);
				}

			}
		}

		if (buildings.get(position).getPreviewResourceImageId() == 0)
		{
			thumb_image.setImageBitmap(BitmapFactory.decodeResource(activityContext.getResources(),
				android.R.drawable.ic_menu_zoom));
		}
		else
		{
			thumb_image.setImageBitmap(BitmapFactory.decodeResource(activityContext.getResources(),
				buildings.get(position).getPreviewResourceImageId()));
		}

		return convertView;
	}
}
